package com.mrd.gtimp.business.action;

import com.mrd.constant.MsgConstants;
import com.mrd.exception.QueryException;
import com.mrd.framework.config.web.json.Result;
import com.mrd.framework.utils.HttpUtils;
import com.mrd.gtimp.base.entity.User;
import com.mrd.gtimp.business.entity.Declaration;
import com.mrd.gtimp.business.entity.DeclarationInform;
import com.mrd.gtimp.business.entity.SendRemind;
import com.mrd.gtimp.business.service.DeclarationBiz;
import com.mrd.gtimp.business.service.DeclarationInformBiz;
import com.mrd.gtimp.business.service.SendRemindBiz;
import com.mrd.util.ModelPrinter;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

/**
 * <p> application name: gtimp <p> copyright: Copyright@2012 明睿达科技有限公司
 */

/**
 * @author yangyl
 */

@Controller
@RequestMapping("/business")
public class SendRemindAction {
    private static final long serialVersionUID = 1L;
    @Resource
    private SendRemindBiz sendRemindBiz;
    @Resource
    private DeclarationInformBiz declarationInformBiz;
    @Resource
    private DeclarationBiz declarationBiz;

    /**
     * 默认访问入口
     */
    @RequestMapping("/sendremind-sendAdd")
    public String sendAdd() {
        String id = HttpUtils.getParam("ID");
        HttpUtils.getRequest().setAttribute("ID", id);
        return "/gtimp/sendremind/sendRemind";
    }

    /**
     * 新增修改信息
     */
    @RequestMapping("/sendremind-saveOrUpdate")
    @ResponseBody
    public Result saveOrUpdate(User user, Declaration declaration, DeclarationInform declarationInform) {
        Result msg = null;
        try {
            String id = HttpUtils.getParam("id");
            // if(StringUtils.isEmpty(sendRemind.getId())) { // 新增操作
            // sendRemind.setId(id);
            // } else { // 修改操作
            // }
            List<String> properties = new ArrayList<String>();
            if (StringUtils.isEmpty(declarationInform.getId())) {
                declaration = declarationBiz.findEntityById(id);
                declarationInform.setId(null);
                declarationInform.setDeclaration(declaration);
                declarationInform.setConfirmState("0");
                declarationInform.setCreateTime(new Date());
                declarationInform.setCreator(user);
                declarationInform.setCustomerCompany(declaration.getCustomer());
                declarationInform.setSupCompany(declaration.getSupCompany());
            } else {
                properties.add("sendRemind");
            }
            if (declarationInform.getSendRemind() != null
                    && StringUtils.isEmpty(declarationInform.getSendRemind().getId())) {
                declarationInform.getSendRemind().setId(null);
            }
            declarationInformBiz.updateEntity(declarationInform, properties);
            msg = Result.newSuccessInstance(MsgConstants.SAVE_SUECCES);
        } catch (Exception e) {
            e.printStackTrace();
            // msg = MsgBuildUtil.appendMsg(MsgConstants.SAVE_FAIL,
            // e.getMessage());
        }
        return msg;
    }

    /**
     * 编辑信息
     */
    @RequestMapping("/sendremind-find")
    public SendRemind find(SendRemind sendRemind) {
        if (null != sendRemind) {
            if (StringUtils.isNotEmpty(sendRemind.getId())) {
                try {
                    sendRemind = sendRemindBiz.findEntityById(sendRemind.getId() + "");
                    return  sendRemind;
                } catch (QueryException e) {
                    e.printStackTrace();
                }
            }
        }
        ModelPrinter.print1(sendRemind);
        return null;
    }

    @Resource
    public void setSendRemindBiz(SendRemindBiz sendRemindBiz) {
        this.sendRemindBiz = sendRemindBiz;
    }


}
